package com.zhaoc.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.zhaoc.web.base.BaseAction;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>title:com.zhaoc.web.action</p>
 * date:2018/8/12 0012
 * time:15:04
 * author:赵成
 * user:13566
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class ImageAction extends BaseAction<Object> {

    //属性驱动获取上传图片的参数
    private File imgFile;
    private String imgFileFileName;
    private String imgFileFileContentType;

    public void setImgFile(File imgFile) {
        this.imgFile = imgFile;
    }

    public void setImgFileFileName(String imgFileFileName) {
        this.imgFileFileName = imgFileFileName;
    }

    public void setImgFileFileContentType(String imgFileFileContentType) {
        this.imgFileFileContentType = imgFileFileContentType;
    }

    @Action(value = "image_upload", results = {@Result(name = SUCCESS, type = "json")})
    public String upload() throws IOException {
        System.out.println("文件:" + imgFile);
        System.out.println("文件名:" + imgFileFileName);
        System.out.println("文件类型:" + imgFileFileContentType);

        //获取保存路径
        String savePath = ServletActionContext.getServletContext().getRealPath("/upload/") + "\\";
        //获取保存连接
        String saveUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";
        //生成随机图片名
        UUID uuid = UUID.randomUUID();
        String exd = imgFileFileName.substring(imgFileFileName.lastIndexOf("."));
        String randomFileName = uuid + exd;

        //以绝对路径保存图片
        FileUtils.copyFile(imgFile, new File(savePath + randomFileName));

        //通知浏览器上传成功
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("error", 0);
        result.put("url", saveUrl + randomFileName);//返回相对路径
        ActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }

    @Action(value = "image_manage", results = {@Result(name = SUCCESS, type = "json")})
    public String manage() {
        //先获取储存图片的目录
        String rootPath = ServletActionContext.getServletContext().getRealPath("/") + "upload\\";
        //获取储存图片的连接
        String rootUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";

        //遍历目录取得文件信息
        List<Map<String, Object>> fileList = new ArrayList<Map<String, Object>>();
        //当前上传目录
        File currentPathFile = new File(rootPath);
        //图片的扩展名
        String[] fileTypes = new String[]{"jpg", "jpeg", "gif", "png", "bmp"};

        if (currentPathFile.listFiles() != null) {
            for (File file : currentPathFile.listFiles()) {
                Map<String, Object> hash = new HashMap<String, Object>();
                String fileName = file.getName();
                if (file.isDirectory()) {
                    hash.put("is_dir", true);
                    hash.put("has_file", (file.listFiles() != null));
                    hash.put("filesize", 0L);
                    hash.put("is_photo", false);
                    hash.put("fileTypr", "");
                } else if (file.isFile()) {
                    //将文件后缀名全部取出来,并全部转为小写.
                    String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
                    hash.put("is_dir", false);
                    hash.put("has_file", false);
                    hash.put("filesize", file.length());
                    hash.put("is_photo", Arrays.<String>asList(fileTypes).contains(fileExt));
                    hash.put("fileType", fileExt);
                }
                hash.put("filename", fileName);
                hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
                fileList.add(hash);
            }

        }

        Map<String,Object> result = new HashMap<String, Object>();
        result.put("moveup_dir_path","");
        result.put("current_dir_path",rootPath);
        result.put("current_url",rootUrl);
        result.put("total_count",fileList.size());
        result.put("file_list",fileList);
        ActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }

}
